define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exam/papers_main/index' + location.search,
                    add_url: 'exam/papers_main/papers_edit',
                    // edit_url: 'exam/papers_main/edit',
                    del_url: 'exam/papers_main/del',
                    multi_url: 'exam/papers_main/multi',
                    import_url: 'exam/papers_main/import',
                    table: 'exam_papers_main',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'describe', title: '简介', operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'realname', title: __('创建者'), operate:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'question',
                                    title: '作业共享',
                                    text: '作业共享',
                                    extend:'data-area=["60%","50%"]',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'exam/papers_main/papers_share'
                                },
                                {
                                    name: 'question',
                                    title: '编辑',
                                    text: '编辑',
                                    extend:'data-area=["100%","100%"]',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'exam/papers_main/papers_edit'
                                },
                                // {
                                //     name: 'question',
                                //     title: '查看习题',
                                //     text: '查看习题',
                                //     extend:'data-area=["90%","90%"]',
                                //     classname: 'btn btn-xs btn-primary btn-dialog',
                                //     icon: 'fa fa-list',
                                //     url: 'exam/papers/question'
                                // },
                                /*{
                                    name: 'question',
                                    title: '分配班级',
                                    text: '分配班级',
                                    extend:'data-area=["90%","90%"]',
                                    classname: 'btn  btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'exam/papers/classes'
                                }*/
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        question: function () {
            var pa_id = $("#pa_id").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exam/papers_main/question?id=' + pa_id,
                    table: 'exam_question',
                }
            });

            var table = $("#table2");

            console.log($.fn.bootstrapTable.defaults.extend.index_url);
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: false,//隐藏导出
                showToggle: false,//隐藏浏览模式
                showColumns: false,//隐藏显示隐藏模式
                searchFormVisible: false,//默认显示搜索表单
                commonSearch: false,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'cate_id', title: __('Cate_id'), autocomplete: false, visible: false},
                        {field: 'cate_name', title: __('所属题库'), operate: false},
                        // {field: 'exam_type_id', title: __('Exam_type_id'), visible: false},
                        // {field: 'examtype.name', title: __('Exam_type_id'), operate: false},
                        {
                            field: 'kind',
                            title: __('试题类型'),
                            // searchList: {"JUDGE": __('Judge'), "SINGLE": __('Single'), "MULTI": __('Multi'), "FILL": __('Fill')},
                            formatter: function(value, row, index){
                                if(value == 'JUDGE')
                                {
                                    return '判断题';
                                }
                                else if(value == 'SINGLE')
                                {
                                    return '单选题';
                                }
                                else if(value == 'MULTI')
                                {
                                    return '多选题';
                                }
                                else if(value == 'FILL')
                                {
                                    return '填空题';
                                }
                                return value;
                            },
                            operate: false
                        },
                        // {field: 'title', title: __('题目'), autocomplete: false, operate: 'LIKE', formatter: Table.api.formatter.content},
                        // {field: 'explain', title: __('Explain'), operate: false},
                        // {field: 'dtrs', title: __('答题人数')},
                        {
                            field: 'difficulty',
                            title: __('难度'),
                            // searchList: {"EASY": __('Easy'), "GENERAL": __('General'), "HARD": __('Hard')},
                            formatter: function(value, row, index){
                                if(value == 'EASY')
                                {
                                    return '简单';
                                }
                                else if(value == 'GENERAL')
                                {
                                    return '普通';
                                }
                                else if(value == 'HARD')
                                {
                                    return '困难';
                                }
                                return value;
                            },
                            operate: false
                        },
                        {
                            field: 'answer', title: __('正确答案'), halign: 'center', align: 'left', operate: false, formatter: function (value, row, index) {
                                var answer = value;
                                if (row.kind == 'FILL') {
                                    try {
                                        answer = '';
                                        let fill_answers = JSON.parse(row.answer);
                                        for (let i = 0; i < fill_answers.length; i++) {
                                            answer += '填空位' + (i + 1) + '：' + fill_answers[i].answers.join('、') + '<br>';
                                        }
                                    } catch (e) {
                                        console.log('answer', index, answer, e);
                                        return value;
                                    }
                                }

                                console.log('answer', value, answer);
                                return answer;
                            }
                        },
                        {
                            field: 'other_url',
                            title: __('拓展链接'),
                            align: 'left',
                            formatter: Controller.api.formatter.url,
                            addClass: "selectpicker"
                        },
                        {field: 'describe', title: __('备注')},
                        {field: 'accuracy', title: __('正确率')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name:"查看",
                                    text: __('查看'),
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'exam/papers/view',
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        classes: function () {
            var pas_id = $("#pas_id").val();
            // console.log(pas_id);
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "班级名称";};
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exam/papers/classes?id=' + pas_id  + location.search ,
                    multi_url: 'classes/classes/multi',
                    dragsort_url: '',
                    table: 'classes_classes',
                }
            });

            var table = $("#table3");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: Config.students_id ? 'Classes.id' : 'id',
                sortName: Config.students_id ? 'Classes.id' : 'id',
                search: true,
                showExport: false,//隐藏导出
                showToggle: false,//隐藏浏览模式
                showColumns: false,//隐藏显示隐藏模式
                searchFormVisible: false,//默认显示搜索表单
                commonSearch: false,
                columns: [
                    [
                        {field: 'title', title: '班级名称'},
                        {field: 'entry_year', title: '入学年份'},
                        {field: 'realname', title: '班级老师',operate: false},
                        {field: 'mobile', title: '手机号',operate: false},
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'jb_class',
                                    title: '解除关联',
                                    text: '解除关联',
                                    confirm: '确认解除？解除后成绩将无法查看',
                                    classname: 'btn btn-xs btn-ajax btn-danger',
                                    icon: 'fa',
                                    url: 'exam/papers/jb_class?paper_id=' + pas_id,
                                    visible:function(row){
                                        // console.log(row);
                                        if(row.status == 1)
                                        {
                                            return true;
                                        }
                                    },
                                    refresh:true
                                },
                                {
                                    name: 'bd_classf',
                                    title: '关联班级',
                                    text: '关联班级',
                                    extend:'data-area=["80%","80%"]',
                                    classname: 'btn  btn-xs btn-info btn-dialog',
                                    icon: 'fa',
                                    url: 'exam/papers/expandclass?paper_id=' + pas_id,
                                    visible:function(row){
                                        if(row.status == 0)
                                        {
                                            return true;
                                        }
                                    },
                                    refresh:false
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'exam/papers/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'exam/papers/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'exam/papers/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        expandclass: function () {
            // Controller.api.bindevent();
            Form.api.bindevent($("form[role=form]"),function (data,ret) {

                if(ret.code == 1)
                {
                    // alert('操作成功');
                    // parent.location.reload();
                    parent.$("#table3").bootstrapTable('refresh',{});
                }
            }, function(data, ret){
                // alert('网络错误！');
            });
            // Fast.api.close(data);
        },
        papers_share: function () {
            // Controller.api.bindevent();
            Form.api.bindevent($("form[role=form]"),function (data,ret) {

                if(ret.code == 1)
                {
                    Fast.api.close(data);
                }
            }, function(data, ret){
                // alert('网络错误！');
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                }
            }
        }
    };
    return Controller;
});
